package cn.circle.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.circle.system.dao.IOrderInfoDao;
//import cn.circle.commom.model.Apartment;
import cn.circle.system.model.CommodityInfo;
import cn.circle.system.model.OrderInfo;
import cn.circle.system.service.IOrderInfoService;
import cn.circle.system.pagemodel.DataGrid;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.util.DateUtils;
import cn.circle.system.util.HqlHelper;
/**
 * 
 * @author
 * 
 */
@Service
public class OrderInfoServiceImpl implements IOrderInfoService {
	@Autowired
	private IOrderInfoDao OrderInfodao;
	
	
	@Override
	public DataGrid datagrid(PageHelper ph, OrderInfo o,String begin_time,String end_time) {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
		List<OrderInfo> cml = new ArrayList<OrderInfo>();
		//用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();
		
		//注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer(
				"from OrderInfo t where 1=1 ");
		//以下为查询条件
		if(o.getPayPrice()!= null && !"".equals(o.getPayPrice())){


			params.put("payPrice",o.getPayPrice());
			hql.append(" and t.payPrice =:payPrice ");
		}
		
		/*if(o.getSyscustomer()!= null && !"".equals(o.getSyscustomer())&& o.getSyscustomer().getCustId()!=0){
			hql.append(" and t.syscustomer.custId="+o.getSyscustomer().getCustId());
		}
		if(o.getCommodityInfo() != null && !"".equals(o.getCommodityInfo())&& o.getCommodityInfo().getCommodityId()!=0){
			hql.append(" and t.commodityInfo.commodityIds="+o.getCommodityInfo().getCommodityId());
		}*/
		if(o.getSendStatus() != null && !"".equals(o.getSendStatus())){
			hql.append(" and t.sendStatus="+o.getSendStatus());
		}
		if(o.getPayStatus() != null && !"".equals(o.getPayStatus())){
			
			hql.append(" and t.payStatus="+o.getPayStatus());
		}
		if(o.getUserName()!= null && !"".equals(o.getUserName())){
			hql.append("and t.userName="+o.getUserName()+"");
		}
		if(o.getOrderNo()!= null && !"".equals(o.getOrderNo())){
			hql.append("and t.orderNo='"+o.getOrderNo()+"'");
		}
		/*if(o.getSysDepartment()!= null && !"".equals(o.getSysDepartment()) && o.getSysDepartment().getDepId()!=0){
			params.put("depId",o.getSysDepartment().getDepId());
			hql.append(" and t.sysDepartment.depId = :depId ");
		}*/
//			
	/*	if(depId != null && !"".equals(depId)){
		  hql.append("  AND FIND_IN_SET(t.sysDepartment.depId,getChildDepId("+depId+")) > 0 ");
	        }*/
	
		
		if (begin_time != null && !"".equals(begin_time)) {
			if (end_time != null && !"".equals(end_time)) {
				params.put("begin_time", DateUtils.getStringToDate(begin_time));
				params.put("end_time", DateUtils.getStringToDate(DateUtils.getDateAddDay(end_time)));
				System.out.println("时间case"+DateUtils.getStringToDate(DateUtils.getDateAddDay(end_time)));
				hql.append(" and t.payTime >= :begin_time and t.payTime <= :end_time");

			} else {
				params.put("begin_time", DateUtils.getStringToDate(begin_time));
				hql.append(" and t.payTime >= :begin_time ");

			}

		} else {
			if (end_time != null && !"".equals(end_time)) {
				params.put("end_time", DateUtils.getStringToDate(end_time));
				hql.append(" and t.payTime >= :end_time ");

			}
		}
		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<OrderInfo> l =  OrderInfodao.find(hql.toString(), params, ph.getPage(),
				ph.getRows());
		dg.setRows(l);
		dg.setTotal(OrderInfodao.count("select count(*) " + hql, params));
		return dg;
	}

	@Override
	public void save(OrderInfo o) {
		// TODO Auto-generated method stub
		OrderInfodao.save(o);
		
	}

	@Override
	public void deleteAll(String ids) {
		// TODO Auto-generated method stub
		OrderInfodao.deleteAll(ids);
	}

	@Override
	public void removeAll(String ids) {
		// TODO Auto-generated method stub
		OrderInfodao.removeAll(ids);
	}

	@Override
	public void update(OrderInfo o) {
		// TODO Auto-generated method stub
		OrderInfodao.updateAll(o);
	}

/*	@Override
	public List<Apartment> getOne(String id) {
		// TODO Auto-generated method stub
		List<Apartment> ll=OrderInfodao.getDep();
		return ll;
	}*/
	@Override
	public void updateStatus(String ids) {
		// TODO Auto-generated method 
	
		OrderInfodao.updateStatus(ids);
		
	}

	@Override
	public List<OrderInfo> getOrder(Long id) {
		// TODO Auto-generated method stub
		String sql="FROM OrderInfo WHERE orderId="+id;
		List<OrderInfo> ll=OrderInfodao.find(sql);
		return ll;
	}

	@Override
	public List<OrderInfo> findMyOrder(Integer custId) {
		// TODO Auto-generated method stub
		String hql="FROM OrderInfo as t WHERE t.syscustomer.custId="+custId+"order by t.orderId DESC";
		List<OrderInfo> ll=OrderInfodao.find(hql);
		return ll;
	}

	@Override
	//待收货
	public List<OrderInfo> waitTheGoods(Integer custId) {
		// TODO Auto-generated method stub
		String hql="FROM OrderInfo as t WHERE t.syscustomer.custId="+custId+"AND t.sendStatus in(2,3) order by t.orderId DESC";
		List<OrderInfo> ll=OrderInfodao.find(hql);
		return ll;
	}

	@Override
	//待发货
	public List<OrderInfo> waitCargo(Integer custId) {
		// TODO Auto-generated method stub
		String hql="FROM OrderInfo as t WHERE t.syscustomer.custId="+custId+"AND t.sendStatus in(1,2) order by t.orderId DESC";
		List<OrderInfo> ll=OrderInfodao.find(hql);
		return ll;
		
	}

	@Override
	public List<OrderInfo> waitComm(Integer custId) {
		// TODO Auto-generated method stub
		String hql="FROM OrderInfo as t WHERE t.syscustomer.custId="+custId+"AND t.commStatus=1 AND t.sendStatus=4 order by t.orderId DESC";
		List<OrderInfo> ll=OrderInfodao.find(hql);
		return ll;
	}

	@Override
	public List<OrderInfo> waitPay(Integer custId) {
		// TODO Auto-generated method stub
				String hql="FROM OrderInfo as t WHERE t.syscustomer.custId="+custId+"AND t.payStatus=2 order by t.orderId DESC";
				List<OrderInfo> ll=OrderInfodao.find(hql);
				return ll;
	}

	@Override
	public void confirm(Long orderId) {
		// TODO Auto-generated method stub
		OrderInfodao.updateSendStatus(orderId);
	}

	

	



}
